{{extend '../_layouts/home.html'}}

{{block 'title'}}{{'多人博客 - 首页'}}{{/block}}
{{block 'head'}}
<style>
  .w-e-toolbar{
    flex-wrap:wrap;
  }
</style>
{{/block}}
{{block 'body'}}
<section class="container">
  <div class="row">
    <div class="col-md-12">
      <form>
        <div class="form-group">
        
          <label for="exampleInputEmail1">选择板块</label>
          
          <select class="form-control" id="selectType">
            <option value="share">分享</option>
            <option value="question">问答</option>
            <option value="recruitment">招聘</option>
          </select>
         
        </div>
        <div class="form-group">
          <label for="exampleInputEmail1">标题</label>
          <input id="userTitle" type="text" class="form-control" id="exampleInputEmail1" placeholder="请输入标题">
        </div>
        <div class="form-group">
          <label for="exampleInputPassword1">内容</label>
          <div id="editor"></div>
        </div>
        <button id="submitBtn" type="button" class="btn btn-default">Submit</button>
      </form>
    </div>
  </div>
</section>
{{/block}}
{{block 'script'}}
<script src="/node_modules/wangeditor/release/wangeditor.js"></script>
<script type="text/javascript">
  var E = window.wangEditor
  var editor = new E('#editor')
  editor.customConfig.uploadImgShowBase64 = true   // 使用 base64 保存图片
  // 或者 var editor = new E( document.getElementById('editor') )
  editor.create();
  //提交表单内容
  $("#submitBtn").click(function (){
    $.ajax({
    type:"post",
    url:"/topics/new",
    data:{
      type:$("#selectType").val(),
      title:$("#userTitle").val().replace("script",""),
      content:editor.txt.html().replace("script","")
    },
    dataType:"json",
    success:(res)=>{
      if(res.err_code==200){
        alert("提交成功")
        window.location.href="/"
      }
      else{
        alert("提交失败，请重新提交");
        
      }
    },
    error:()=>{
      alert("提交失败！")
    }
  })
});
</script>
{{/block}}
